package com.easy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.*;
import com.github.yulichang.wrapper.MPJLambdaWrapper;

import com.easy.bean.*;
import com.easy.service.*;
import com.easy.util.*;
import com.easy.exception.*;

@RestController
@RequestMapping("/reserve")
@CrossOrigin
public class ReserveController {

    @Autowired
    private ReserveService reserveService;



    @GetMapping
    public Result getAll() {
        return Result.success(reserveService.list());
    }

    @GetMapping("page")
    public Result getPage(Page page, @RequestParam(name = "search", required = false) String searchtext) {
         MPJLambdaWrapper<Reserve> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAssociation(Course.class,Reserve::getCourse);
        wrapper.rightJoin(Course.class, Course::getId, Reserve::getCourseId);
        wrapper.selectAssociation(Member.class,Reserve::getMember);
        wrapper.rightJoin(Member.class, Member::getId, Reserve::getMemberId);
        if (searchtext != null && !searchtext.isEmpty()) {
            wrapper.like("status", searchtext);
            wrapper.or();
            wrapper.like("score", searchtext);
            wrapper.or();
            wrapper.like("comment", searchtext);
            wrapper.or();
            wrapper.like("commentTime", searchtext);
            wrapper.or();
            wrapper.like("reserveTime", searchtext);
            wrapper.or();
            wrapper.like(Course::getName, searchtext);
            wrapper.or();
            wrapper.like(Member::getName, searchtext);
        }
        return Result.success(reserveService.page(page, wrapper));
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Long id) {
        return Result.success(reserveService.getById(id));
    }

    @PostMapping
    public Result create(@RequestBody Reserve entity) {
        return Result.success("数据已经存储",reserveService.save(entity));
    }

    @PutMapping
    public Result update( @RequestBody Reserve entity) {
        return Result.success("数据已经更新",reserveService.saveOrUpdate(entity));
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id) {
        return Result.success("数据已经删除",reserveService.removeById(id));
    }
}
